package com.hxkj.service.impl;

import com.hxkj.client.service.*;
import com.hxkj.domain.po.*;
import com.hxkj.entity.vo.SiteItem;
import com.hxkj.entity.vo.SiteType;
import com.hxkj.entity.vo.WebsiteAddrDetail;
import com.hxkj.service.SiteService;
import com.platform.common.domain.to.PageTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * created by kezhijie on 2018/9/5 15:14.
 * email to kezhijie@hxoss.com
 */
@Service
public class SiteServiceImpl implements SiteService {

    @Autowired
    private DictTypeServiceClient dictTypeServiceClient;

    @Autowired
    private DictItemServiceClient dictItemServiceClient;

    @Autowired
    private SiteServiceClient siteServiceClient;

    @Autowired
    private WebsiteAddrServiceClient websiteAddrServiceClient;

    @Autowired
    private UserSiteCollectServiceClient userSiteCollectServiceClient;
    @Autowired
    private UniversalServiceClient universalServiceClient;
    @Value("${storage.imgUrl}")
    private String imgIPPORT;

    final String[] parentTypes = new String[]{"产业金融", "产业经济", "产业服务"};

    @Override
    public List<SiteType> getList(String userId) throws Exception {
        PageTo pageParam = new PageTo();
        pageParam.setPageNum(1);
        pageParam.setPageSize(10);

        UserSiteCollect userSiteCollect = new UserSiteCollect();
        userSiteCollect.setUserId(userId);

        List<UserSiteCollect> userSiteCollects = userSiteCollectServiceClient.get(userSiteCollect);
        List<Long> collectSiteList = userSiteCollects.stream().map(p -> p.getSiteId()).collect(Collectors.toList());

        List<DictType> parents = new ArrayList<>();
        for (String type : parentTypes) {

            DictType dictType = new DictType();
            dictType.setDictName(type);

            parents.add((dictTypeServiceClient.getDictType(dictType).getData()));
        }

        List<SiteType> siteTypes = new ArrayList<>();

        for (DictType parent : parents) {
            SiteType siteType = new SiteType();
            if (StringUtils.hasText(parent.getComment()))
                siteType.setComment(parent.getComment());
            if (Objects.nonNull(parent.getCreateTime()))
                siteType.setCreateTime(parent.getCreateTime());
            if (Objects.nonNull(parent.getDelTag()))
                siteType.setDelTag(parent.getDelTag());
            if (StringUtils.hasText(parent.getDictName()))
                siteType.setDictName(parent.getDictName());
            if (Objects.nonNull(parent.getDictTypeCode()))
                siteType.setDictTypeCode(parent.getDictTypeCode());
            if (Objects.nonNull(parent.getDictTypeId()))
                siteType.setDictTypeId(parent.getDictTypeId());
            if (Objects.nonNull(parent.getUpdateTime()))
                siteType.setUpdateTime(parent.getUpdateTime());

            DictItem data = new DictItem();
            data.setDictTypeCode(siteType.getDictTypeCode());

            List<DictItem> dictItems = dictItemServiceClient.getDictItemByDictType(data).getData();

            List<SiteItem> siteItems = new ArrayList<>();

            // 如果没有二级类型，需要把一级类型复制成二级类型 kezhijie
            if (dictItems.size() > 0) {
                for (DictItem dictItem : dictItems) {
                    SiteItem siteItem = buildSiteItem(dictItem);

                    Site site = new Site();
                    site.setSiteType(siteItem.getDictItemCode().intValue());

                    List<Site> sites = siteServiceClient.getAllSite(site);

                    List<WebsiteAddr> websiteAddrs = new ArrayList<>();

                    List<WebsiteAddrDetail> websiteAddrDetails = new ArrayList<>();

                    for (Site site1 : sites) {
                        WebsiteAddr websiteAddrs1 = new WebsiteAddr();
                        websiteAddrs1.setSiteId(site1.getSiteId());
                        websiteAddrs.addAll(websiteAddrServiceClient.getList(websiteAddrs1));
                    }

                    for (WebsiteAddr websiteAddr : websiteAddrs) {
                        WebsiteAddrDetail websiteAddrDetail = buildWebsiteAddrDetail(websiteAddr);
                        Site site1 = new Site();
                        site1.setSiteId(websiteAddr.getSiteId());
                        Site site2 = siteServiceClient.getSite(site1).getData();
                        SiteUniversal siteUniversalCondition = new SiteUniversal();
                        siteUniversalCondition.setSiteId(websiteAddr.getSiteId());
                        SiteUniversal siteUniversalInfo = universalServiceClient.get(siteUniversalCondition).getData();
                        String logo = imgIPPORT + siteUniversalInfo.getLogo2();

                        websiteAddrDetail.setCollect(collectSiteList.contains(websiteAddrDetail.getSiteId()));
                        websiteAddrDetail.setLogo(logo);
                        websiteAddrDetail.setSiteName(site2.getSiteName());
                        websiteAddrDetail.setComment(site2.getComment());
                        websiteAddrDetails.add(websiteAddrDetail);
                    }

                    siteItem.setWebsiteAddrs(websiteAddrDetails);
                    siteItems.add(siteItem);
                }
            }

            siteType.setChildren(siteItems);

            siteTypes.add(siteType);
        }

        return siteTypes;
    }

    @Override
    public List<WebsiteAddr> getAddress() {
        return null;
    }

    private SiteItem buildSiteItem(DictItem dictItem) {
        SiteItem siteItem = new SiteItem();

        if (Objects.isNull(dictItem))
            return siteItem;

        if (StringUtils.hasText(dictItem.getComment()))
            siteItem.setComment(dictItem.getComment());
        if (Objects.nonNull(dictItem.getCreateTime()))
            siteItem.setCreateTime(dictItem.getCreateTime());
        if (Objects.nonNull(dictItem.getDelTag()))
            siteItem.setDelTag(dictItem.getDelTag());
        if (Objects.nonNull(dictItem.getDictItemCode()))
            siteItem.setDictItemCode(dictItem.getDictItemCode());
        if (Objects.nonNull(dictItem.getDictItemId()))
            siteItem.setDictItemId(dictItem.getDictItemId());
        if (StringUtils.hasText(dictItem.getDictItemName()))
            siteItem.setDictItemName(dictItem.getDictItemName());
        if (Objects.nonNull(dictItem.getDictItemCode()))
            siteItem.setDictTypeCode(dictItem.getDictTypeCode());
        if (Objects.nonNull(dictItem.getUpdateTime()))
            siteItem.setUpdateTime(dictItem.getUpdateTime());
        return siteItem;
    }

    private WebsiteAddrDetail buildWebsiteAddrDetail(WebsiteAddr websiteAddr) {
        WebsiteAddrDetail websiteAddrDetail = new WebsiteAddrDetail();

        if (Objects.isNull(websiteAddr))
            return websiteAddrDetail;

        if (Objects.nonNull(websiteAddr.getAddrId()))
            websiteAddrDetail.setAddrId(websiteAddr.getAddrId());
        if (Objects.nonNull(websiteAddr.getAddrIds()))
            websiteAddrDetail.setAddrIds(websiteAddr.getAddrIds());
        if (Objects.nonNull(websiteAddr.getCreateor()))
            websiteAddrDetail.setCreateor(websiteAddr.getCreateor());
        if (Objects.nonNull(websiteAddr.getCreateTime()))
            websiteAddrDetail.setCreateTime(websiteAddr.getCreateTime());
        if (Objects.nonNull(websiteAddr.getDelTag()))
            websiteAddrDetail.setDelTag(websiteAddr.getDelTag());
        if (StringUtils.hasText(websiteAddr.getIpAdd())){
            if (!websiteAddr.getIpAdd().startsWith("http://"))
                websiteAddrDetail.setIpAdd("http://" + websiteAddr.getIpAdd());
            else
                websiteAddrDetail.setIpAdd(websiteAddr.getIpAdd());
        }
        if (StringUtils.hasText(websiteAddr.getIpPort()))
            websiteAddrDetail.setIpPort(websiteAddr.getIpPort());
        if (Objects.nonNull(websiteAddr.getModifyBy()))
            websiteAddrDetail.setModifyBy(websiteAddr.getModifyBy());
        if (Objects.nonNull(websiteAddr.getSiteId()))
            websiteAddrDetail.setSiteId(websiteAddr.getSiteId());
        if (StringUtils.hasText(websiteAddr.getSourcesDirectory()))
            websiteAddrDetail.setSourcesDirectory(websiteAddr.getSourcesDirectory());
        if (StringUtils.hasText(websiteAddr.getTargetName()))
            websiteAddrDetail.setTargetName(websiteAddr.getTargetName());
        if (Objects.nonNull(websiteAddr.getUpdateTime()))
            websiteAddrDetail.setUpdateTime(websiteAddr.getUpdateTime());
        if (StringUtils.hasText(websiteAddr.getUrlPrefix()))
            websiteAddrDetail.setUrlPrefix(websiteAddr.getUrlPrefix());
        if (StringUtils.hasText(websiteAddr.getUrlRegular()))
            websiteAddrDetail.setUrlRegular(websiteAddr.getUrlRegular());
        return websiteAddrDetail;
    }
}
